/*
 * Copyright (c) 2010, Oracle. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * * Redistributions of source code must retain the above copyright notice,
 *   this list of conditions and the following disclaimer.
 *
 * * Redistributions in binary form must reproduce the above copyright notice,
 *   this list of conditions and the following disclaimer in the documentation
 *   and/or other materials provided with the distribution.
 *
 * * Neither the name of Oracle nor the names of its contributors
 *   may be used to endorse or promote products derived from this software without
 *   specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
 * THE POSSIBILITY OF SUCH DAMAGE.
 */
package examples;

import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.Dispatch;
import com.jacob.com.DispatchEvents;
import com.jacob.com.Variant;
import java.util.Observable;
import java.util.Observer;

import javax.swing.JTextField;

public class KepwareClientTest extends javax.swing.JFrame implements Observer {

    ActiveXComponent dotnetCom;
    Dispatch com;

    TagEvent tagEvent;

    DispatchEvents de;

    JTextField[] TagGroup = new JTextField[4];
    JTextField[] TagValueGroup = new JTextField[4];
//    JTextField[] TagQualityGroup = new JTextField[4];
//    JTextField[] TagDataTypeGroup = new JTextField[4];

    /**
     * Creates new form Antenna
     */
    public KepwareClientTest() {
        initComponents();

        initControlGroup();

        dotnetCom = new ActiveXComponent("KepwareClientCOM.KepwareClient");
        com = (Dispatch) dotnetCom.getObject();
    }

    private void initControlGroup() {
        TagGroup[0] = Tag1;
        TagGroup[1] = Tag2;
        TagGroup[2] = Tag3;
        TagGroup[3] = Tag4;

        TagValueGroup[0] = Tag1_Value;
        TagValueGroup[1] = Tag2_Value;
        TagValueGroup[2] = Tag3_Value;
        TagValueGroup[3] = Tag4_Value;

//        TagQualityGroup[0] = Tag1_Quality;
//        TagQualityGroup[1] = Tag2_Quality;
//        TagQualityGroup[2] = Tag3_Quality;
//        TagQualityGroup[3] = Tag4_Quality;
//
//        TagDataTypeGroup[0] = Tag1_DataType;
//        TagDataTypeGroup[1] = Tag2_DataType;
//        TagDataTypeGroup[2] = Tag3_DataType;
//        TagDataTypeGroup[3] = Tag4_DataType;
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jButton_SubscripEvent = new javax.swing.JButton();
        jButton_ReadTag1 = new javax.swing.JButton();
        jButton_ReadTag2 = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jTextField_Host = new javax.swing.JTextField();
        jButton_ConnectHost = new javax.swing.JButton();
        jPanel_TagList = new javax.swing.JPanel();
        jLabel3 = new javax.swing.JLabel();
        Tag1 = new javax.swing.JTextField();
        jLabel8 = new javax.swing.JLabel();
        Tag1_Value = new javax.swing.JTextField();
        jLabel11 = new javax.swing.JLabel();
        Tag2 = new javax.swing.JTextField();
        jLabel12 = new javax.swing.JLabel();
        Tag2_Value = new javax.swing.JTextField();
        jLabel17 = new javax.swing.JLabel();
        Tag3 = new javax.swing.JTextField();
        jLabel18 = new javax.swing.JLabel();
        Tag3_Value = new javax.swing.JTextField();
        jLabel21 = new javax.swing.JLabel();
        Tag4 = new javax.swing.JTextField();
        jLabel22 = new javax.swing.JLabel();
        Tag4_Value = new javax.swing.JTextField();
        Value1 = new javax.swing.JTextField();
        Value2 = new javax.swing.JTextField();
        Value3 = new javax.swing.JTextField();
        Value4 = new javax.swing.JTextField();
        Write1_jButton = new javax.swing.JButton();
        Write2_jButton = new javax.swing.JButton();
        Write3_jButton = new javax.swing.JButton();
        Write4_jButton = new javax.swing.JButton();
        jPanel3 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        Log = new javax.swing.JTextArea();

        jButton_SubscripEvent.setText("Subscrip Event");
        jButton_SubscripEvent.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton_SubscripEventActionPerformed(evt);
            }
        });

        jButton_ReadTag1.setText("Read");
        jButton_ReadTag1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton_ReadTag1ActionPerformed(evt);
            }
        });

        jButton_ReadTag2.setText("Read");
        jButton_ReadTag2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton_ReadTag2ActionPerformed(evt);
            }
        });

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("KepwareClientTest");

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Configuration"));

        jLabel1.setText("Kepware Server IP");

        jTextField_Host.setText("localhost");

        jButton_ConnectHost.setText("Connect");
        jButton_ConnectHost.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton_ConnectHostActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout jPanel1Layout = new org.jdesktop.layout.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .add(jLabel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 116, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(jTextField_Host, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 214, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .add(18, 18, 18)
                .add(jButton_ConnectHost, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 103, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(210, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                .add(jLabel1)
                .add(jTextField_Host, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .add(jButton_ConnectHost))
        );

        jPanel_TagList.setBorder(javax.swing.BorderFactory.createTitledBorder("Tag List"));

        jLabel3.setText("Tag");

        Tag1.setText("Channel1.Device1.testFloat");

        jLabel8.setText("Value");

        Tag1_Value.setEditable(false);

        jLabel11.setText("Tag");

        Tag2.setText("Channel1.Device1.testBoolean");

        jLabel12.setText("Value");

        Tag2_Value.setEditable(false);

        jLabel17.setText("Tag");

        Tag3.setText("Channel1.Device1.testString");

        jLabel18.setText("Value");

        Tag3_Value.setEditable(false);

        jLabel21.setText("Tag");

        Tag4.setText("Channel1.Device1.testInt");

        jLabel22.setText("Value");

        Tag4_Value.setEditable(false);

        Write1_jButton.setText("Write");
        Write1_jButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                Write1_jButtonActionPerformed(evt);
            }
        });

        Write2_jButton.setText("Write");
        Write2_jButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                Write2_jButtonActionPerformed(evt);
            }
        });

        Write3_jButton.setText("Write");
        Write3_jButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                Write3_jButtonActionPerformed(evt);
            }
        });

        Write4_jButton.setText("Write");
        Write4_jButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                Write4_jButtonActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout jPanel_TagListLayout = new org.jdesktop.layout.GroupLayout(jPanel_TagList);
        jPanel_TagList.setLayout(jPanel_TagListLayout);
        jPanel_TagListLayout.setHorizontalGroup(
            jPanel_TagListLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel_TagListLayout.createSequentialGroup()
                .addContainerGap()
                .add(jPanel_TagListLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
                    .add(jPanel_TagListLayout.createSequentialGroup()
                        .add(jLabel21, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 28, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(Tag4))
                    .add(jPanel_TagListLayout.createSequentialGroup()
                        .add(jPanel_TagListLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
                            .add(jLabel3, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 27, Short.MAX_VALUE)
                            .add(jLabel11, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(jPanel_TagListLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
                            .add(Tag1)
                            .add(Tag2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 184, Short.MAX_VALUE)))
                    .add(jPanel_TagListLayout.createSequentialGroup()
                        .add(jLabel17, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 28, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(Tag3)))
                .add(18, 18, 18)
                .add(jPanel_TagListLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
                    .add(jPanel_TagListLayout.createSequentialGroup()
                        .add(jLabel22)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                        .add(Tag4_Value))
                    .add(jPanel_TagListLayout.createSequentialGroup()
                        .add(jLabel18)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                        .add(Tag3_Value))
                    .add(jPanel_TagListLayout.createSequentialGroup()
                        .add(jLabel8)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                        .add(Tag1_Value, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 88, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                    .add(jPanel_TagListLayout.createSequentialGroup()
                        .add(jLabel12)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                        .add(Tag2_Value)))
                .add(18, 18, 18)
                .add(jPanel_TagListLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
                    .add(Value1)
                    .add(Value2)
                    .add(Value3)
                    .add(Value4, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 88, Short.MAX_VALUE))
                .add(18, 18, 18)
                .add(jPanel_TagListLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(Write1_jButton)
                    .add(Write2_jButton)
                    .add(Write3_jButton)
                    .add(Write4_jButton))
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel_TagListLayout.setVerticalGroup(
            jPanel_TagListLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel_TagListLayout.createSequentialGroup()
                .add(jPanel_TagListLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel3)
                    .add(Tag1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(jLabel8)
                    .add(Tag1_Value, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(Value1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(Write1_jButton))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(jPanel_TagListLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel11)
                    .add(Tag2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(jLabel12)
                    .add(Tag2_Value, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(Value2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(Write2_jButton))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(jPanel_TagListLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel17)
                    .add(Tag3, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(jLabel18)
                    .add(Tag3_Value, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(Value3, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(Write3_jButton))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(jPanel_TagListLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jPanel_TagListLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                        .add(jLabel21)
                        .add(jLabel22)
                        .add(Tag4, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                    .add(jPanel_TagListLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                        .add(Tag4_Value, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .add(Value4, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .add(Write4_jButton)))
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder("Log"));

        Log.setColumns(20);
        Log.setRows(5);
        jScrollPane1.setViewportView(Log);

        org.jdesktop.layout.GroupLayout jPanel3Layout = new org.jdesktop.layout.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jScrollPane1)
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, jPanel3Layout.createSequentialGroup()
                .add(0, 0, Short.MAX_VALUE)
                .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 179, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
        );

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jPanel1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .add(jPanel_TagList, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .add(jPanel3, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .add(jPanel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(jPanel_TagList, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(jPanel3, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        jPanel1.getAccessibleContext().setAccessibleName("Connection");

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton_ConnectHostActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton_ConnectHostActionPerformed

        String buttonText = jButton_ConnectHost.getText();

        if (buttonText.equals("Connect")) {

            tagEvent = new TagEvent();
            tagEvent.addObserver(this);
            de = new DispatchEvents(com, tagEvent);

            String hostString = jTextField_Host.getText();
//            Variant[] arguArray = new Variant[]{new Variant(hostString)};
//            Dispatch.call(com, "SetHost", arguArray);

            Variant[] arguArray = new Variant[]{};
            Variant result = Dispatch.call(com, "Connect", arguArray);
            writeLog("Connect to Server:" + hostString);

            jButton_ConnectHost.setText("Disconnect");
        } else {

            de.safeRelease();
            tagEvent = null;

            Variant[] arguArray = new Variant[]{};
            Variant result = Dispatch.call(com, "Disconnect", arguArray);
            writeLog("Disconnect to Server");
            jButton_ConnectHost.setText("Connect");
        }

    }//GEN-LAST:event_jButton_ConnectHostActionPerformed
    private synchronized void writeLog(String loginfo) {

        Log.append("\r\n" + loginfo + "\r\n");

    }
    private void jButton_SubscripEventActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton_SubscripEventActionPerformed


    }//GEN-LAST:event_jButton_SubscripEventActionPerformed

    private void jButton_ReadTag1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton_ReadTag1ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jButton_ReadTag1ActionPerformed

    private void jButton_ReadTag2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton_ReadTag2ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jButton_ReadTag2ActionPerformed

    private void Write1_jButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_Write1_jButtonActionPerformed
        // TODO add your handling code here:
        Variant value = new Variant(this.Value1.getText());
        Variant tag = new Variant(Tag1.getText());
        Variant[] arguArray = new Variant[]{tag, value};
        Variant result = Dispatch.call(com, "WriteTagValue", arguArray);

    }//GEN-LAST:event_Write1_jButtonActionPerformed

    private void Write2_jButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_Write2_jButtonActionPerformed
        // TODO add your handling code here:
        Variant value = new Variant(this.Value2.getText());
        Variant tag = new Variant(Tag2.getText());
        Variant[] arguArray = new Variant[]{tag, value};
        Variant result = Dispatch.call(com, "WriteTagValue", arguArray);

    }//GEN-LAST:event_Write2_jButtonActionPerformed

    private void Write3_jButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_Write3_jButtonActionPerformed
        // TODO add your handling code here:
        Variant value = new Variant(this.Value3.getText());
        Variant tag = new Variant(Tag3.getText());
        Variant[] arguArray = new Variant[]{tag, value};
        Variant result = Dispatch.call(com, "WriteTagValue", arguArray);
    }//GEN-LAST:event_Write3_jButtonActionPerformed

    private void Write4_jButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_Write4_jButtonActionPerformed
        // TODO add your handling code here:
        Variant value = new Variant(this.Value4.getText());
        Variant tag = new Variant(Tag4.getText());
        Variant[] arguArray = new Variant[]{tag, value};
        Variant result = Dispatch.call(com, "WriteTagValue", arguArray);
    }//GEN-LAST:event_Write4_jButtonActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {


        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new KepwareClientTest().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextArea Log;
    private javax.swing.JTextField Tag1;
    private javax.swing.JTextField Tag1_Value;
    private javax.swing.JTextField Tag2;
    private javax.swing.JTextField Tag2_Value;
    private javax.swing.JTextField Tag3;
    private javax.swing.JTextField Tag3_Value;
    private javax.swing.JTextField Tag4;
    private javax.swing.JTextField Tag4_Value;
    private javax.swing.JTextField Value1;
    private javax.swing.JTextField Value2;
    private javax.swing.JTextField Value3;
    private javax.swing.JTextField Value4;
    private javax.swing.JButton Write1_jButton;
    private javax.swing.JButton Write2_jButton;
    private javax.swing.JButton Write3_jButton;
    private javax.swing.JButton Write4_jButton;
    private javax.swing.JButton jButton_ConnectHost;
    private javax.swing.JButton jButton_ReadTag1;
    private javax.swing.JButton jButton_ReadTag2;
    private javax.swing.JButton jButton_SubscripEvent;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel17;
    private javax.swing.JLabel jLabel18;
    private javax.swing.JLabel jLabel21;
    private javax.swing.JLabel jLabel22;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel_TagList;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextField jTextField_Host;
    // End of variables declaration//GEN-END:variables

    public void update(Observable o, Object arg) {
        Variant[] vArray = (Variant[]) arg;
        for (Variant v : vArray) {
            String data = v.toString();
            String[] dataArray = data.split(":");
            updateTagControl(dataArray[0], dataArray[1], dataArray[2], dataArray[3]);
        }

    }

    public void updateTagControl(String tagName, String tagValue, String quality, String datatype) {

        for (int i = 0; i < TagGroup.length; i++) {
            JTextField tag = TagGroup[i];
            if (tag.getText().equals(tagName)) {
                TagValueGroup[i].setText(tagValue);
//                TagQualityGroup[i].setText(quality);
//                TagDataTypeGroup[i].setText(datatype);
            }
        }
        StringBuilder sbuilder = new StringBuilder();

        sbuilder.append("UpdateData for    [" + tagName + "]");
        sbuilder.append("\r\n                    Value [" + tagValue + "]");
        sbuilder.append("\r\n                  Quality [" + quality + "]");
        sbuilder.append("\r\n             DataType [" + datatype + "]");

        writeLog(sbuilder.toString());
    }
}
